From 55ea3e415b31b834004bee9f5367fbfb420bbeef Mon Sep 17 00:00:00 2001 From: Armand Philippot Date: Mon, 18 Dec 2023 15:22:14 +0100 Subject: refactor(pages): merge Github/Gitlab overview on project pages By using conditional fetchning we can avoid to duplicate the ProjectOverview component and be more accurate about what data is loading. --- src/pages/projets/[slug].tsx | 239 +++++++++++++++++++------------------------ 1 file changed, 104 insertions(+), 135 deletions(-) (limited to 'src/pages/projets/[slug].tsx') diff --git a/src/pages/projets/[slug].tsx b/src/pages/projets/[slug].tsx index 958f61f..b29c477 100644 --- a/src/pages/projets/[slug].tsx +++ b/src/pages/projets/[slug].tsx @@ -1,9 +1,10 @@ +/* eslint-disable max-statements */ import type { MDXComponents } from 'mdx/types'; import type { GetStaticPaths, GetStaticProps } from 'next'; import dynamic from 'next/dynamic'; import Head from 'next/head'; import NextImage from 'next/image'; -import { useMemo, type ComponentType, type FC } from 'react'; +import { useMemo, type ComponentType, useCallback } from 'react'; import { useIntl } from 'react-intl'; import { Heading, @@ -20,7 +21,7 @@ import { Spinner, Time, getLayout, - type ProjectOverviewProps, + type OverviewMeta, } from '../../components'; import { mdxComponents } from '../../components/mdx'; import { fetchGithubRepoMeta } from '../../services/github'; @@ -30,7 +31,6 @@ import type { Maybe, NextPageWithLayout, Project, - ProjectMeta, } from '../../types'; import { CONFIG } from '../../utils/config'; import { @@ -71,107 +71,6 @@ const getGithubRepoInputFrom = (namespace: string) => { const isValidRepo = (name: string): name is 'github' | 'gitlab' => ['github', 'gitlab'].includes(name); -type GithubRepoOverviewProps = Omit< - ProjectOverviewProps, - 'cover' | 'meta' | 'name' -> & - Pick & { - repos: { - github: string; - gitlab?: string; - }; - title: string; - }; - -const GithubRepoOverview: FC = ({ - cover, - license, - repos, - technologies, - title, - ...props -}) => { - const intl = useIntl(); - const { isLoading, meta: repoMeta } = useGithubRepoMeta( - getGithubRepoInputFrom(repos.github) - ); - const reposLabels = { - github: intl.formatMessage({ - defaultMessage: 'Github', - description: 'ProjectPage: Github repo label', - id: 'l82UU5', - }), - gitlab: intl.formatMessage({ - defaultMessage: 'Gitlab', - description: 'ProjectPage: Gitlab repo label', - id: '1msHuZ', - }), - }; - const stars = intl.formatMessage( - { - defaultMessage: - '{starsCount, plural, =0 {No stars} one {# star} other {# stars}}', - description: 'ProjectPage: stars count', - id: '4M71hp', - }, - { starsCount: repoMeta?.stargazerCount } - ); - const popularityURL = `https://github.com/${repos.github}/stargazers`; - - return isLoading ? ( - - {intl.formatMessage({ - defaultMessage: 'Loading the repository metadata...', - description: 'ProjectPage: loading repository metadata', - id: 'EET/tC', - })} - - ) : ( - : undefined} - meta={{ - creationDate: repoMeta?.createdAt ? ( -